package com.itboye.jinpuyiyuan.activity.home.healthconsultation;

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.bean.ArticleDetailBean;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.FlowLayout;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.zip.Inflater;

/**
 * Created by Administrator on 2017/12/13 0013.
 * 文章
 */

public class HealthConsultationDetailActivity extends BaseActivity implements Observer {
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;
    private int num=5;

    private TextView tableLeftTv,tableRightTv;//点赞数
    private BranchAddressPresenter presenter;
    private ImageView likeImg;//点赞
    private ImageView healthImg;
    private String uid;//用户uid
    private PopupWindow likePop;//点赞pop
    private TextView sureTv,cancelTv,contentTv,contentDetail;
    private FlowLayout tagLl;//tags
    private String islike;
    private String articleId;

    private int cancelStye;// 0点赞  1 取消点赞
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_healthconsultationdetail);
        initViewData();

    }

    private void initViewData() {
        leftTv.setVisibility(View.GONE);
        rightImg.setVisibility(View.GONE);
        uid= (String) SPUtils.get(getApplication(),null, Const.UID,"");
        islike=getIntent().getStringExtra("islike");
        articleId = getIntent().getStringExtra("articleId");

//        updatLike(false);
        presenter=new BranchAddressPresenter(this);

        presenter.articleDetail(articleId,uid);

//        rightImg.setBackgroundDrawable(getResources().getDrawable(android.R.drawable));

    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;
            //文章点赞
            case R.id.tableRightTv:
            case R.id.likeImg:
                likeImg.setEnabled(false);
                tableRightTv.setEnabled(false);
                if (cancelStye==1){
                    startLikePop();
                }else {
                    cancelStye=0;
                    presenter.postLike(articleId,uid,0);
                }
                break;
            // 点赞pop 点击事件
            case R.id.sureTv:
                presenter.postLike(articleId,uid,0);

                break;
            case R.id.cancelTv:
               likePop.dismiss();
                likeImg.setEnabled(true);
                tableRightTv.setEnabled(true);
                break;
        }
    }

     //点赞pop
    private  void startLikePop(){
        View inflate = LayoutInflater.from(this).inflate(R.layout.popwind_likearticle, null);
        likePop=new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        likePop.setAnimationStyle(R.style.popupwindow_anim_style);
        sureTv=inflate.findViewById(R.id.sureTv);
        cancelTv=inflate.findViewById(R.id.cancelTv);
        contentTv=inflate.findViewById(R.id.contentTv);
        contentTv.setText("已经点过赞了,确定取消点赞");
        sureTv.setText("确定");
        cancelTv.setText("取消");
        likePop.setFocusable(true);
        inflate.setFocusable(true);//comment by danielinbiti,设置view能够接听事件，标注1
        inflate.setFocusableInTouchMode(true); //comment by danielinbiti,设置view能够接听事件 标注2
        sureTv.setOnClickListener(this);
        cancelTv.setOnClickListener(this);
        likePop.showAtLocation(inflate, Gravity.TOP,0,0);
    }
    @Override
    public void update(Observable o, Object arg) {
        ResultEntity resultEntity = handlerError(arg);
        if (resultEntity!=null){

            if (resultEntity.getEventType()==BranchAddressPresenter.By_Post_like_success){
                presenter.articleDetail(articleId,uid);
                likeImg.setEnabled(true);
                tableRightTv.setEnabled(true);

//              if (cancelStye==0){
//                  updatLike(true);
//                  MAlert.alert("点赞成功");
//                  cancelStye=1;
//              }else{
//                  updatLike(false);
//                  MAlert.alert("取消点赞成功");
//                  cancelStye=0;
//              }
              if (likePop!=null){
                  likePop.dismiss();
              }

            }else if (resultEntity.getEventType()==BranchAddressPresenter.By_Post_like_fail){

                MAlert.alert(resultEntity.getMsg());
            }else if (resultEntity.getEventType()== BranchAddressPresenter.By_Post_info_success){

                ArticleDetailBean bean = (ArticleDetailBean) resultEntity.getData();
                tvTitle.setText(bean.getAuthor_name());
                contentTv.setText(bean.getTitle());
                tableRightTv.setText(bean.getLikes());
                Glide.with(this).load(Const.ZX+bean.getCover()).into(healthImg);
                if (tagLl.getChildCount()>0){
                    tagLl.removeAllViews();
                }
                initTags(bean.getTags());
                contentDetail.setMovementMethod(ScrollingMovementMethod.getInstance());// 设置可滚动
                contentDetail.setText(Html.fromHtml(bean.getContent()));

                if (bean.getIs_like().equals("0")){
                    cancelStye=0;
                    updatLike(false);
                }else {
                    cancelStye=1;
                    updatLike(true);
                }
            }else if (resultEntity.getEventType()==BranchAddressPresenter.By_Post_info_fail){

            }

        }

    }

    private void initTags(List<String> tags) {
        for (int i=0;i<tags.size();i++){
//            android:layout_width="wrap_content"
//            android:layout_height="wrap_content"
//            android:paddingLeft="@dimen/DIMEN_16DP"
//            android:paddingRight="@dimen/DIMEN_16DP"
//            android:paddingBottom="@dimen/DIMEN_8DP"
//            android:paddingTop="@dimen/DIMEN_8DP"
//            android:text="关键字"
//            android:textSize="@dimen/DIMEN_12SP"
//            android:textColor="@color/black"
//            android:layout_marginRight="@dimen/DIMEN_10DP"
//            android:background="@drawable/login_corner_whitesolid"
            TextView textView = new TextView(this);
            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            textView.setPadding(16,8,16,8);
            params.setMargins(10,0,10,0);
            textView.setText(tags.get(i));
            textView.setTextColor(Color.BLACK);
            textView.setTextSize(12);
            textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_corner_whitesolid));
            textView.setLayoutParams(params);
            tagLl.addView(textView);
        }
    }

    //改变点赞状态 true 点赞  false 取消或默认没有点赞
    public void updatLike(boolean isLike){

        if (isLike){
           likeImg.setImageDrawable(getResources().getDrawable(R.drawable.zanwenzhangxuanze));
        }else {
            likeImg.setImageDrawable(getResources().getDrawable(R.drawable.zanwenzhang));
        }
    }
}
